using System;
using System.Collections;

namespace HtmlParser
{
	/// <summary>
	/// Summary description for ParseUtils.
	/// </summary>
	public class ParserUtils
	{
		public static void MakeXmlValid(HtmlParser.Document document)
		{
			foreach (HtmlParser.Nodes.Node node in document.RootNodes)
			{
				if (node is HtmlParser.Nodes.Element)
				{
					MakeXmlValid((HtmlParser.Nodes.Element)node);
				}
			}
		}

		public static void MakeXmlValid(HtmlParser.Nodes.Element element)
		{
			element.TagName = element.TagName.ToLower();
			if (element.EndElement != null)
			{
				element.EndElement.TagName = element.EndElement.TagName.ToLower();
			}
			else
			{
				element.IsStandAlone = true;
			}
			
			foreach(HtmlParser.Attribute attrib in element.Attributes)
			{
				attrib.Name = attrib.Name.ToLower();
				attrib.Value = attrib.Value.Replace("\"", "&quot;");
			}

			foreach(HtmlParser.Nodes.Node subElement in element.ChildNodes)
			{
				if (subElement is HtmlParser.Nodes.Element)
				{
					MakeXmlValid((HtmlParser.Nodes.Element)subElement);
				}
			}
		}


		public static void FixCommonUnfinishedTags(HtmlParser.Document document)
		{
			foreach (HtmlParser.Nodes.Node node in document.RootNodes)
			{
				if (node is HtmlParser.Nodes.Element)
				{
					FixCommonUnfinishedTags((HtmlParser.Nodes.Element)node);
				}
			}
		}

		public static void FixCommonUnfinishedTags(HtmlParser.Nodes.Element element)
		{
			FixUnfinishedTags(element,"tr","td");
			FixUnfinishedTags(element,"ul","li");
			FixUnfinishedTags(element,"ol","li");
			FixUnfinishedTags(element,"map","area");
		}

		public static void FixUnfinishedTags(HtmlParser.Nodes.Element element,string containerTagName,string unfinishedTagName)
		{
			if (element.TagName.ToLower() == containerTagName)
			{
				ArrayList orphans = new ArrayList();
				ArrayList newParents = new ArrayList();
				Nodes.Element badElement = null;

				foreach (HtmlParser.Nodes.Node subElement in element.ChildNodes)
				{					
					if (subElement is HtmlParser.Nodes.Element)
					{
						HtmlParser.Nodes.Element tagElement = (HtmlParser.Nodes.Element)subElement;

						if (tagElement.TagName.ToLower() == unfinishedTagName)
						{
							if ((tagElement.EndElement == null) && (!tagElement.IsStandAlone))
							{
								badElement = tagElement;

								HtmlParser.Nodes.Element endElement = new HtmlParser.Nodes.Element("</" + unfinishedTagName + ">");
								badElement.EndElement = endElement;
							}
							else
							{
								badElement = null;
							}
						}
						else
						{
							if (badElement != null)
							{
								orphans.Add(subElement);
								newParents.Add(badElement);
							}
						}
					}
					else
					{
						if (badElement != null)
						{							
							orphans.Add(subElement);
							newParents.Add(badElement);
						}
					
					}
				}

				for (int i = 0; i < orphans.Count; i++)
				{
					HtmlParser.Nodes.Element parent = (HtmlParser.Nodes.Element)newParents[i];
					parent.AppendChild((HtmlParser.Nodes.Node)orphans[i]);
				}
			}

			foreach(HtmlParser.Nodes.Node subElement in element.ChildNodes)
			{
				if (subElement is HtmlParser.Nodes.Element)
				{
					FixUnfinishedTags((HtmlParser.Nodes.Element)subElement, containerTagName, unfinishedTagName);
				}
			}
		}

		public static void FindElements(HtmlParser.Nodes.Element parentElement, string elementName, bool recursive)
		{
			foreach (HtmlParser.Nodes.Node node in parentElement.ChildNodes)
			{ 

			}
		}
	}
}
